<template>
    <div class="user-assets-box">

        <div class="title">提现</div>
        <div class="field">
            <mt-field label="金额" placeholder="请输入提现金额" type="number" v-model="number">
                <mt-button type="primary" size="small" @click="submit">提现</mt-button>
            </mt-field>
        </div>

        <div class="title">提现记录</div>

        <div class="lists">
            <item
                    v-for="(item, k) of lists"
                    border="10">
                <div class="items">
                    <div class="info">
                        <div class="gold">
                            <span style="color: #FF8D24">&#xe934;</span>
                            提现金额：{{item.number}}
                        </div>
                        <div class="time">
                            {{item.time}}
                        </div>
                    </div>
                    <div class="status">
                        <mt-button
                                type="default"
                                size="small"
                                v-if="item.status === 0"
                                @click="cancel(k)">
                            <span v-if="!isLoading(k)">取消</span>
                            <mt-spinner
                                    v-else
                                    type="fading-circle"
                                    color="black"></mt-spinner>
                        </mt-button>
                        <span v-else-if="item.status === 1" style="color: #55a532">已完成</span>
                        <span v-else style="color: #ccc">已取消</span>
                    </div>
                </div>
            </item>
        </div>
        <next-page
                :loading="loading"
                :more="full"
                :data="lists.length===0"
                @more="loadMore"></next-page>
    </div>
</template>

<script>
    import apiUrl from '@/api/apiUrl'
    import {Toast} from 'mint-ui'
    import nextPage from '../../../components/nextPage'
    import Item from '../../../components/item'

    export default {
        name: "index",
        data () {
            return {
                userGold: {},
                number: 0,
                lists: [],
                loading: false,
                full: false,
                page: 1,
                row: 20,
                actLoading: []
            }
        },
        beforeRouteEnter(to, from, next) {
            next(vm => {
                vm.setData(to.query)
                vm.setLists()
            })
        },
        components: {
            nextPage,
            Item
        },
        methods: {
            isLoading(k) {
                return this.actLoading.some(v => {
                    return v === k
                })
            },
            cancel(k){
                this.actLoading.push(k)

                let params = {
                    params: {
                        id: this.lists[k].id
                    }
                }

                this.$http.get(apiUrl.api.cashCancel, params).then(res => {
                    if (res.data.code === 0) {
                        this.lists[k].status = 2
                        Toast('已取消提现')
                    } else {
                        Toast('取消失败')
                    }
                    let arr = this.actLoading.filter(v => {
                        return v !== k
                    })
                    this.actLoading = arr
                }).catch(err => console.log(err))
            },
            submit() {
                if (this.number <= 0) {
                    return Toast(`提现金额必须大于0`)
                }
                if (this.number > this.userGold.number) {
                    return Toast(`最大提现金额:${this.userGold.number}金币`)
                }

                let params = new URLSearchParams();
                params.append('room_id', this.$route.query.room_id)
                params.append('number', this.number)

                this.$http.post(apiUrl.api.cash, params).then(res => {
                    if (res.data.code === 0) {
                        this.lists.unshift(res.data.result)
                    } else {
                        Toast(res.data.msg)
                    }
                }).catch(err => console.log(err))
            },
            loadMore() {
                if (this.lists.length < this.row) {
                    this.loading = false
                } else {
                    this.loading = true
                    this.$set(this, 'page', this.page + 1)
                    this.setLists(this.page, this.row, res => {
                        this.loading = false;
                        if (res.length === 0) {
                            this.$set(this, 'full', true)
                        }
                    })
                }
            },
            setLists (page = 1, row = 20, fun = null) {
                let params = {
                    params: {
                        page: page,
                        row: row
                    }
                }
                this.$http.get(apiUrl.api.cashLists, params).then(res => {
                    if (res.data.code === 0) {
                        this.$set(this, 'lists', this.lists.concat(res.data.result))
                        if (fun !== null) {
                            fun(res.data.result)
                        }
                        if (res.data.result.length < 20) {
                            this.full = true
                        }
                    } else {
                        Toast(res.data.msg)
                    }
                }).catch(err => {
                    console.log(err)
                })
            },
            setData (q) {
                let params = {
                    params: q
                }
                this.$http.get(apiUrl.api.user_gold, params).then(res => {
                    if (res.data.code !== 0) {
                        Toast(res.data.msg)
                    } else {
                        this.userGold = res.data.result
                    }
                }).catch(err => {
                    console.log(err)
                })
            }
        }
    }
</script>

<style scoped lang="less">
    @import "../../../assets/css/bases";
    .title {
        height: 100px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: @big;
        color: @gray;
    }
    .lists {
        display: flex;
        flex-direction: column;
        width: 100%;
        border-top: #f1f1f1 solid 1px;
        .items {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .time {
                color: @gray;
                line-height: 35px;
            }
            .frequency {
                color: @gray;
            }
        }
    }
</style>